package com.example.covid19.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.covid19.common.Dto.Editdto;
import com.example.covid19.common.lang.Result;
import com.example.covid19.entity.Dynamique;
import com.example.covid19.entity.Enum.MessageTemplate;
import com.example.covid19.entity.StationMessage;
import com.example.covid19.entity.Student;
import com.example.covid19.mapper.DynamiqueMapper;
import com.example.covid19.service.DynamiqueService;
import com.example.covid19.service.StationMessageService;
import com.example.covid19.service.StudentService;
import com.example.covid19.util.JiGuangRestSend;
import com.example.covid19.util.cron.CronUtil;
import com.example.covid19.util.tencent.Metixing;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.text.ParseException;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author BaiZhengChun
 * @since 2023-03-07
 */
@RestController
@RequestMapping("/dynamique")
public class DynamiqueController {
    private final DynamiqueService dynamiqueService;
    private final DynamiqueMapper dynamiqueMapper;
    @Autowired
    Metixing metixing;
    @Autowired
    JiGuangRestSend jiGuangRestSend;
    @Autowired
    StudentService studentService;
    @Autowired
    StationMessageService stationMessageService;

    public DynamiqueController(DynamiqueService dynamiqueService, DynamiqueMapper dynamiqueMapper) {
        this.dynamiqueService = dynamiqueService;
        this.dynamiqueMapper = dynamiqueMapper;
    }

    public String changecron(String cron) throws ParseException {
        return CronUtil.parseDate(CronUtil.nextTime(cron, new Date()));
    }

    /**
     * Cron转日期,格式变为时间 new SimpleDateFormat("hh:mm:ss");
     *
     * @throws ParseException
     */
    @RequestMapping("/test")
    public void test() throws ParseException {
        Dynamique dynamique = dynamiqueService.getOne(new QueryWrapper<Dynamique>().eq("id", 3));
        System.out.println(CronUtil.parseDate(CronUtil.nextTime(dynamique.getCron(), new Date())));
    }

    /**
     * 日期转Cron
     */
    @RequestMapping("/chsendtime")
    public void chesendtime(){
        Date date = new Date();
        Dynamique dynamique = dynamiqueService.getOne(new QueryWrapper<Dynamique>().eq("id",3));
        System.out.println(CronUtil.parseCron(date));
    }
    @RequestMapping("/addtime")
    public void addtime(@RequestBody Editdto editdto){
        Date date = new Date();
        long time = date.getTime();
        long hour = 1000*60*editdto.getShijian();
        long minute = 1000*editdto.getShijian();
        if (editdto.getBiaoshi() == 1) {
            date.setTime(time + hour);
        } else {
            date.setTime(time + minute);
        }
//        date.setTime(time + 1000*60);
        date.setTime(time + 1000 * 60 * 60);
        Dynamique dynamique = dynamiqueService.getOne(new QueryWrapper<Dynamique>().eq("id", 3));
        System.out.println(CronUtil.parseCron(date));
    }

    /**
     * 获取定时任务信息
     *
     * @return
     */
    @RequestMapping("getlist")
    public Result getlist() {
        List<Dynamique> list = dynamiqueService.list();
        list.forEach(v -> {
            try {
                v.setCron(changecron(v.getCron()));
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        });
        return Result.success(list);
    }

    /**
     * @param id
     * @param state
     * @return开启和关闭定时任务
     */
    @RequestMapping("Cdystate")
    public Result Cdystate(@RequestParam("id") String id, @RequestParam("state") Integer state) {
        Dynamique dynamique = dynamiqueService.getById(id);
        dynamique.setIsEnable(state);
        dynamiqueService.updateById(dynamique);
        return Result.success(dynamique);
    }

    /**
     * @param id
     * @param state
     * @return 将更改后的时间转换成cron表达式并保存
     * @throws Exception
     */
    @RequestMapping("chtimer")
    public Result chtimer(@RequestParam("id") String id, @RequestParam("state") String state) throws Exception {
        LocalTime time = LocalTime.parse(state, DateTimeFormatter.ofPattern("HH:mm:ss"));
        // "ss mm HH * * ?" 要转换的 cron 表达式的格式
        String cronStr = time.format(DateTimeFormatter.ofPattern("ss mm HH * * ?"));
        Dynamique dynamique = dynamiqueService.getById(id);
        dynamique.setCron(cronStr);
        boolean flag = dynamiqueService.updateById(dynamique);
        if (flag) {
            return Result.success(200, "成功", null);
        } else {
            return Result.fail(400, "失败", null);
        }

    }


}
